/****************************************************************************
 * File: mainwindow.cpp
 *
 * Date: 2010-02-18
 * Description: Main GUI.
 ***************************************************************************/

#include <QtGui>
#include "mainwindow.h"

MainWindow::MainWindow()
{
//-----------------------------------------------------------------------------
    QFont font;
    font.setFamily("Courier");
    font.setFixedPitch(true);
    font.setPointSize(14);

    textEdit = new QTextEdit;
    textEdit->setFont(font);
    textEdit->setReadOnly(true);

    QFile file("README.txt");
    if (file.open(QFile::ReadOnly | QFile::Text))
        textEdit->setPlainText(file.readAll());
//-----------------------------------------------------------------------------

    baseStationCheckBox = new QCheckBox("Download as Base Station", this);

    setWindowTitle( tr("Application batch Downloader") );
    setFixedSize(600, 600);

    //This add on 2010-02-20 to inhance the GUI
    //****************************** penguinx ***********************
    QGroupBox *mainGroup = new QGroupBox(this);
    mainGroup->setTitle(tr("Operations"));

    QGroupBox *commandGroup = new QGroupBox(mainGroup);
    commandGroup->setAttribute(Qt::WA_ContentsPropagated);
    commandGroup->setTitle(tr("Step 1. Download Protocol"));
    QPushButton *detectNodeButton = new QPushButton(tr("Detect"), commandGroup);
    QPushButton *downloadPreButton = new QPushButton(tr("Download"), commandGroup);
    nodeIDLabel = new QLabel(QString("Download Node ID: "), this);

    QGroupBox *downloadGroup = new QGroupBox(mainGroup);
    downloadGroup->setAttribute(Qt::WA_ContentsPropagated);
    downloadGroup->setTitle(tr("Step 2. Download Application"));
    QPushButton *applicationButton = new QPushButton(tr("Find Application..."), downloadGroup);
    applicationLineEdit = new QLineEdit;
    QPushButton *insertApplicationButton = new QPushButton(tr("Insert Application..."), downloadGroup);
    QPushButton *checkBaseButton = new QPushButton(tr("Check Base Station"), downloadGroup);
    QPushButton *downloadButton = new QPushButton(tr("Disseminate"), downloadGroup);
    QLabel *tipLabel = new QLabel("Please wait for at least \n5 minutes before \"reprogram \nthe network\"", this);
    QPushButton *reprogramDownloadButton = new QPushButton(tr("Reprogram Network"), downloadGroup);

    // Layouts
    mainGroup->setFixedWidth(220);
    QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
    mainGroupLayout->addWidget(commandGroup);
    mainGroupLayout->addStretch(1);
    mainGroupLayout->addWidget(downloadGroup);
    mainGroupLayout->addStretch(2);

    QVBoxLayout *commandGroupLayout = new QVBoxLayout(commandGroup);
    commandGroupLayout->addWidget(detectNodeButton);
    commandGroupLayout->addWidget(baseStationCheckBox);
    commandGroupLayout->addWidget(downloadPreButton);
    commandGroupLayout->addWidget(nodeIDLabel);

    QVBoxLayout *downloadGroupLayout = new QVBoxLayout(downloadGroup);
    downloadGroupLayout->addWidget(checkBaseButton);
    downloadGroupLayout->addWidget(applicationButton);
    downloadGroupLayout->addWidget(applicationLineEdit);
    downloadGroupLayout->addWidget(insertApplicationButton);
    downloadGroupLayout->addWidget(downloadButton);
    downloadGroupLayout->addWidget(tipLabel);
    downloadGroupLayout->addWidget(reprogramDownloadButton);

    connect(detectNodeButton, SIGNAL(clicked()), this, SLOT(detect()));
    connect(downloadPreButton, SIGNAL(clicked()), this, SLOT(downloadPre()));

    connect(applicationButton, SIGNAL(clicked()), this, SLOT(openApplication()));
    connect(insertApplicationButton, SIGNAL(clicked()), this, SLOT(insertApplication()));
    connect(checkBaseButton, SIGNAL(clicked()), this, SLOT(checkBase()));
    connect(reprogramDownloadButton, SIGNAL(clicked()), this, SLOT(reprogramDownload()));
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(download()));

    QFrame *frame = new QFrame;
    QHBoxLayout *mainLayout = new QHBoxLayout(frame);
    mainLayout->addWidget(mainGroup);
    mainLayout->addWidget(textEdit);

    setCentralWidget(frame);
}

MainWindow::~MainWindow()
{
    delete textEdit;

    delete applicationLineEdit;
}

void MainWindow::detect()
{
    QProcess::execute("motelist");
}

void MainWindow::downloadPre()
{
    static int number = 0;
    nodeIDLabel->setText(QString("Download Node ID: %1").arg(number));

    if (Qt::Checked == baseStationCheckBox->checkState())
    {
	QProcess::execute(QString("./makeb.sh %1").arg(number));
	number++;
#if 0
	QProcess::execute("cd /opt/tinyos-2.1.0/tos/lib/tosboot/");
	QProcess::execute("make telosb");
	QProcess::execute("cd /opt/tinyos-2.1.0/apps/tests/deluge/GoldenImage");
	QProcess::execute("CFLAGS=-DDELUGE_BASESTATION make telosb install,0 bsl,/dev/ttyUSB0");
#endif
    }
    else
    {
	QProcess::execute(QString("./make.sh %1").arg(number));
	number++;
    }
}

void MainWindow::openApplication()
{
    // Open the application XML
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
		    "/opt/tinyos-2.1.0/apps/", tr("XML files(*.xml)")); 
    applicationLineEdit->setText(fileName);
}

void MainWindow::insertApplication()
{
    QString applicationXML = applicationLineEdit->text();
    if ( !applicationXML.isNull() && !applicationXML.isEmpty() )
    {
	QProcess::execute("tos-deluge /dev/ttyUSB0 telosb -i 1 " + applicationXML);
    }
}

void MainWindow::checkBase()
{
    QProcess::execute("tos-deluge /dev/ttyUSB0 telosb -p 1");
}

void MainWindow::download()
{
    QProcess::execute("tos-deluge /dev/ttyUSB0 telosb -d 1");
}

void MainWindow::reprogramDownload()
{
    QProcess::execute("tos-deluge /dev/ttyUSB0 telosb -dr 1");
}
